﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
using _2009035.Models;
using _2009035.Utilities;

namespace _2009035.Controllers
{
    //[RequireHttps]
    public class DepartmentController : Controller
    {
        private DbEntities db = new DbEntities();

        private System.Func<Department, GridDepartment> secondarySelector = x => new GridDepartment() { departmentName = x.departmentName, departmentNumber = x.departmentNumber };

        //
        // GET: /Department/

   
        //
        // GET: /Department/Details/5

        public ActionResult Details(string id = null)
        {
            Department department = db.Departments.Find(id);
            if (department == null)
            {
                return HttpNotFound();
            }
            return View(department);
        }

        /// <summary>
        /// Gets the departments.
        /// </summary>
        /// <param name="departmentName">Name of the department.</param>
        /// <returns>
        /// JSON array of the number of departments
        /// </returns>
        public ActionResult DepartmentInformation(string departmentName)
        {
            if (!string.IsNullOrEmpty(departmentName))
            {
                // get the deparment instance from the db
                Departments depts = (Departments)Enum.Parse(typeof(Departments), departmentName);
                string cleanDepartment = Utility.GetEnumDescription(depts);
                Department dbDepartment = db.Departments.FirstOrDefault(x => x.departmentName == cleanDepartment);
                var departmentManager = this.GetDepartmentManagerLog(dbDepartment.departmentNumber).ToList()[0].Employee;

                GridDepartment department = new GridDepartment()
                {
                    departmentName = dbDepartment.departmentName,
                    departmentNumber = dbDepartment.departmentNumber,
                    departmentManager = string.Format("{0} {1}", departmentManager.firstName, departmentManager.lastName),
                    
                };

                department.currentEmployeeCount = db.DepartmentEmployees.Where(x => x.departmentNumber == dbDepartment.departmentNumber && x.toDate == Constants.EndDateAsCurrent).Count();

                return View(department);
            }
             return this.Json(new { departmentNumber="001", currentEmployeeCount=1}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GenerateDepartmentBasedGenderDemographic(string departmentNumber)
        {           
           var currentEmployees = db.DepartmentEmployees.Where(x => x.departmentNumber == departmentNumber && x.toDate == Constants.EndDateAsCurrent).Select(x => x.Employee);
           decimal currentMale = currentEmployees.Where(x => x.gender == "M").Count();
           decimal currentFemale = currentEmployees.Where(x => x.gender == "F").Count();
           decimal total = currentMale + currentFemale;

           List<GridGraph> arrGraph = new List<GridGraph>();
           arrGraph.Add(new GridGraph("Male", Math.Round((currentMale / total) * 100, 4), true));
           arrGraph.Add(new GridGraph("Female", Math.Round((currentFemale / total) * 100, 4), true));

           return Json(new { Data = arrGraph.ToArray(), ChartTitle = "Employee Gender Distribution" }, JsonRequestBehavior.AllowGet);
        }

        private IQueryable<DepartmentManager> GetDepartmentManagerLog(string departmentNumber)
        {
            return db.DepartmentManagers.Where(x => x.departmentNumber == departmentNumber).OrderByDescending(x => x.toDate);
        }

        public ActionResult GetEmployeeTurnoverPerYear(string departmentNumber)
        {
            // returns a summary of the number of employees in the department per year
            // check for a total of 5 years?
            List<int> years = new List<int>();
            int currentYear = DateTime.Now.Year;

            for (int i = 0; i < 5; i++)
            {
                years.Add(currentYear - i);
            }

            // get the count of employees in the departments for each of the years passed
            var employeesInDepartments = db.DepartmentEmployees.Where(x => x.departmentNumber == departmentNumber);


            return null;
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}